package com.xintonghua.bussiness.ui.fragment.client.user;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.alibaba.fastjson.JSONObject;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.xintonghua.base.utils.RefreshUtils;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.adapter.ClubCardAdapter;
import com.xintonghua.bussiness.adapter.GoodAdapter;
import com.xintonghua.bussiness.adapter.SearchProductAdapter;
import com.xintonghua.bussiness.api.HttpCent;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.ClubCardBean;
import com.xintonghua.bussiness.bean.Contant;
import com.xintonghua.bussiness.bean.GoodsManageBean;
import com.xintonghua.bussiness.bean.GoodsManageResponse;
import com.xintonghua.bussiness.bean.UserInfoBean;
import com.xintonghua.bussiness.util.JsonUtil;
import com.xintonghua.bussiness.util.JumpUtils;
import com.xintonghua.bussiness.util.MyState;
import com.xintonghua.bussiness.util.MyUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by liqi on 2018/3/31.
 * 搜索
 */

public class SearchProductActivity extends BaseActivity implements XRecyclerView.LoadingListener {

    @BindView(R.id.recycler_club_card)
    XRecyclerView lvGoods;
    @BindView(R.id.et_search_product)
    EditText etSearchProduct;
    SearchProductAdapter adapter;
    GoodsManageResponse response;
    private List<GoodsManageBean> manageBeans = new ArrayList<>();
    String productName = "";
    int page = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_product);
        ButterKnife.bind(this);
        RefreshUtils.initList(SearchProductActivity.this, lvGoods);
        onSimpleActionBar();
        setSimpleActionBar("搜索结果", "搜索", new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (TextUtils.isEmpty(etSearchProduct.getText().toString().trim())) {
                    MyUtils.mToast(SearchProductActivity.this, "搜索内容不能为空!");
                    return;
                }
                productName = etSearchProduct.getText().toString().trim();
                onRefresh();
            }
        });
        initUI();
    }
    @Override
    public void initUI() {
        super.initUI();
        lvGoods.setPullRefreshEnabled(true);
        lvGoods.setLoadingMoreEnabled(true);
        adapter = new SearchProductAdapter(this, manageBeans);
        lvGoods.setAdapter(adapter);
        lvGoods.setLoadingListener(this);

        onRefresh();
    }
    @Override
    public void onRefresh() {
        page = 1;
        getList();
    }
    @Override
    public void onLoadMore() {
        getList();
        page++;
    }
    public void getList() {
        httpCent.stockProducts(page, "" + productName, -1,
                this, MyState.requestOne);
    }

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        try {
            switch (order) {
                case MyState.requestOne:
                    if (page == 1) {
                        manageBeans.clear();
                    }
                    response = JsonUtil.getEntityByJsonString(data.toString(), GoodsManageResponse.class);
//                    response = JSONObject.parseObject((String) data, GoodsManageResponse.class);
                    Log.i("aaa", "list大小" + response.getList().size());
                    manageBeans.addAll(response.getList());
                    adapter.notifyDataSetChanged();
                    RefreshUtils.onRefresh(adapter, lvGoods);
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.i("aaa", "数据错误");
        }


    }
}
